﻿using System;
using System.Windows.Forms;
using System.Net;
using SledgeHammer;

namespace wump
{
    public partial class FrmWump : Form
    {
        Wumper wumper;

        public FrmWump()
        {
            InitializeComponent();
            
            //initialize the wumper
            wumper = new Wumper();
            
            //fill the list
            FillList();
        }

        private void FillList()
        {
            //clear out the list before filling it
            lstSites.Items.Clear();

            //step through the array
            foreach (string site in wumper.Sites)
            {
                lstSites.Items.Add(site.Trim());
            }
        }

        #region Manualy Wump Sites

        private void btnRunStop_Click(object sender, EventArgs e)
        {
            //initialize the progress bar
            pgbScanning.Maximum = wumper.Sites.Count;
            pgbScanning.Value = 0;

            //intialize the result box
            txtResults.Text = wumper.LogTestStart();

            //loop through the sites to check each one
            foreach (string site in wumper.Sites)
            {
                //run the test
                RunIndividualTest(site);
            }

            //update the log the wump is compelted
            txtResults.Text += wumper.LogTestStop();

            MessageBox.Show("Wump Completed", "Wump Completed", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private bool RunIndividualTest(string site)
        {
            //run the test
            string siteTest = wumper.TestSite(site);

            //update the GUI
            txtResults.Text += siteTest + Environment.NewLine;
            
            pgbScanning.Value++;
            txtResults.Refresh();
            pgbScanning.Refresh();

            //return if the test was a success
            return (siteTest == "success");
        }

        #endregion

        #region Menu Options

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //close the form ending the program
            this.Close();
        }

        private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //open the settings dialog box
            using (FrmSettings frmSettings = new FrmSettings(wumper))
            {
                //if the user hit the ok button
                if (frmSettings.ShowDialog() == DialogResult.OK)
                {
                    //reload the wumper
                    wumper = new Wumper();

                    //reload the sites in the list
                    FillList();
                }
            }            
        }

        #endregion        
    }
}
